//
//  HMOrginalView.swift
//  WeiBo
//
//  Created by apple on 16/6/26.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

// 告诉同学们一个简便方法: 定义数字都定义成 CGFloat类型的
// 有特殊需要的话再更改
let HomeCellMargin: CGFloat = 10


/// 原创微博视图
class HMOrginalView: UIView {

    var bottomConstraint: Constraint?
    
    
    var orginalViewModel: HMStatusCellViewModel?{
    
        didSet{
            
            photoImageView.sd_setImageWithURL(orginalViewModel?.photoUrl)
            nameLabel.text = orginalViewModel?.name
            levelImageView.image = orginalViewModel?.levelImage
            verfityImageView.image = orginalViewModel?.verifyImage
//            contentLabel.text = orginalViewModel?.content
            contentLabel.attributedText = orginalViewModel?.attributeString
            
            // 设置来源字段
            sourceLabel.text = orginalViewModel?.sourceString
            // 设置时间
            timeLabel.text = orginalViewModel?.createtimeString
            
            // 先卸载约束
            bottomConstraint?.uninstall()
            
            //  cell重用导致约束会重
            
            if orginalViewModel?.model?.pic_urls?.count > 0 {
                
                // 显示配图
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    // 为cell的重用做准备
                   self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                // 把配图视图的数据传递过去
                pictureView.pic_urls = orginalViewModel?.model?.pic_urls
                
            } else {
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                  self.bottomConstraint =  make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                // 隐藏配图
            }
        }
        
    }
    
    // 第一步 -- 重写构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步 -- 定义一个方法,来实现 视图的添加和布局
    private func setupUI(){
        
        addSubview(photoImageView)
        addSubview(levelImageView)
        addSubview(verfityImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        // 设置约束 -- 一定要固定死一个控件,然后其他的控件都是参照与这个控件
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(self.snp_top).offset(HomeCellMargin)
            
            make.width.equalTo(40)
            make.height.equalTo(40)
            // 怕由于snapkit的版本原因不起作用
//            make.height.width.equalTo(40)
        }
        
        verfityImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.left.equalTo(nameLabel.snp_right).offset(HomeCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.top.equalTo(nameLabel.snp_bottom).offset(0.5 * HomeCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel.snp_centerY)
            make.left.equalTo(timeLabel.snp_right).offset(HomeCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(photoImageView.snp_left)
            make.top.equalTo(photoImageView.snp_bottom).offset(HomeCellMargin)
//            make.right.equalTo(self.snp_right).offset(HomeCellMargin)
            // 设置width -- 为什么建议设置width
            make.width.equalTo(UIScreen.mainScreen().bounds.width - 2 * HomeCellMargin)
            
            // 设置底部
//            make.bottom.equalTo(self.snp_bottom)
            make.bottom.equalTo(pictureView.snp_top)
        }
        
        // 分析问题的时候,要知道自己刚才改了哪些代码
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            
            // 可以先给他一个固定宽高
            make.width.equalTo(100)
            make.height.equalTo(100)
            
        }
        
        // 自动计算行高最重要的是高度视图的底部是哪里
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
        
    }
    
    // 第三步: 懒加载控件
    
///  头像
    private lazy var photoImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
        
    }()
    
    private lazy var nameLabel: UILabel = UILabel(text: "昵称", fontSize: 17, textColor: UIColor.darkGrayColor(), isSingleLine: true)
    
    /// 昵称
//    private lazy var nameLabel: UILabel = {
//    
//        let label = UILabel()
//        
//        label.text = "昵称"
//        
//        label.textColor = UIColor.darkGrayColor()
//        label.font = UIFont.systemFontOfSize(17)
//        
//        
//        
//        return label
//        
//    }()
    ///时间
    private lazy var timeLabel: UILabel = {
    
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        
        label.text = "时间"
        
        label.font = UIFont.systemFontOfSize(14)
        
        return label
        
    }()
    /// 来源
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        
        label.text = "来源"
        
        label.font = UIFont.systemFontOfSize(14)
        
        return label
        
    }()
    
    /// 等级
    private lazy var levelImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
    }()
    
    /// 认证
    private lazy var verfityImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
    }()
    /// 微博内容
    private lazy var contentLabel: UILabel = {
    
        let lable = UILabel()
        
        
        lable.text = "fadshjkfaldshfjkadhslkfhadskl"
        
        lable.font = UIFont.systemFontOfSize(16)
        
        lable.numberOfLines = 0
        
        lable.textColor = UIColor.blackColor()
        
        return lable
        
    }()

    private lazy var pictureView: HMPictureView = HMPictureView()
}
